package com.base.app_core.ui;

import android.content.Intent;
import android.net.Uri;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.base.app_core.App;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.base.app_core.util.Preferences;
import com.blankj.utilcode.util.StringUtils;


public class AndroidToJs extends Object {

    // 定义JS需要调用的方法
    // 被JS调用的方法必须加入@JavascriptInterface注解
    @JavascriptInterface
    public void jumpToAli() {
        System.out.println("JS调用了Android的hello方法");
        String advertiUrl = Preferences.getDefault().getString(AppConstants.SP.ADVERTISE_URL, null);
        if (StringUtils.isEmpty(advertiUrl)) {
            return;
        }
        if (XYBAUtils.checkAliApkExist(App.getContext())) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(advertiUrl));
                App.getContext().startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(App.getContext(), "请安装支付宝", Toast.LENGTH_SHORT).show();
        }
    }
}
